import Constants from '../constants/Constants'
import Dispatcher from '../Dispatcher'
import {EventEmitter} from 'events'
import merge from 'lodash.merge'
import ajax from '../ajax'

const ActionTypes = Constants.ActionTypes

let DATA_RETRIEVE = 'data_retrieve'

let resources = {}

let ResourceStore = merge({}, EventEmitter.prototype, {

  addDataRetrieveListener(callback) {
    this.once(DATA_RETRIEVE, () => {
      callback && callback(resources)
    })
  },

  setResources (data) {
    resources = data
  },

  fetch() {
    return ajax({
      url: '/api/resources/getPagination'
    }).done(resp => {
      if (resp && 0 === resp.code) {
        this.setResources(resp.rows)
        this.emit(DATA_RETRIEVE)
      }
    })
  }

})

export default ResourceStore

